package com.yanqu.road.server.cmd.activity.douluoshrek;

import com.yanqu.road.pb.activity.DouLuoShrekProto;
import com.yanqu.road.server.cmd.base.AbstractCommand;
import com.yanqu.road.server.command.Cmd;
import com.yanqu.road.server.gameplayer.GamePlayer;
import com.yanqu.road.server.gameplayer.module.activity.shrek.DouLuoShrekModule;
import com.yanqu.road.server.manger.activity.shrek.DouLuoShrekMgr;
import com.yanqu.road.message.YanQuMessage;
import com.yanqu.road.server.protocol.GameErrorCode;
import com.yanqu.road.server.protocol.Protocol;

@Cmd(code = Protocol.S_DOULUOSHREK_ENTER, desc = "进入大场景要开始同步")
public class ShrekEnterSceneCmd extends AbstractCommand {


    @Override
    public boolean parseData(YanQuMessage packet) throws Exception {
        DouLuoShrekProto.EnterShrekReqMsg reqMsg = DouLuoShrekProto.EnterShrekReqMsg.parseFrom(packet.getBytes());
        return true;
    }

    @Override
    public void execute(GamePlayer player, YanQuMessage packet) throws Exception {
        if(!DouLuoShrekMgr.activityInTime()){
            ret = GameErrorCode.E_ACTIVITY_OVER_TIME;
            return;
        }
        Object locker = player.getModule(DouLuoShrekModule.class).getLocker();
        synchronized (locker){
            DouLuoShrekProto.EnterShrekRespMsg.Builder respMsg = player.getModule(DouLuoShrekModule.class).enterScene(player.getUserId());
            player.sendPacket(Protocol.U_DOULUOSHREK_ENTER_SCENE, respMsg);
        }
    }

    @Override
    public void sendData(GamePlayer player) {
        if(ret != 0){
            DouLuoShrekProto.EnterShrekRespMsg.Builder respMsg = DouLuoShrekProto.EnterShrekRespMsg.newBuilder();
            respMsg.setRet(ret);
            player.sendPacket(Protocol.U_DOULUOSHREK_ENTER_SCENE, respMsg);
        }
    }
}
